var degrees = [...Array(360).keys()]

let asyncData = new Object;
async function getData() {
  axios.get('http://39.106.227.22:8081/author/55720/relations?minLevel=1&maxLevel=4')
  .then(function (response) {
    asyncData = response.data.data;
    console.log(asyncData);
    init();
  })
  .catch(function (error) {
    console.log(error);
  });
}

const init = () => {
  const authorName = 'Daniel Andre Vaquero';
  const authorId = 10001;

  // 需要添加name和data
  const seriesSet = {
    type: 'scatter',
    label: {
      normal: {
        show: true,//显示
        formatter: function(params){
          return params.value[4];
        },
        position: 'bottom'
      }
    },
    coordinateSystem: 'polar',
    symbolSize: function (val) {
      return val[2] * 2;
    },
    itemStyle: {
      emphasis: {
        shadowBlur: 10,
        shadowColor: '#fff'
      }
    },
  };

  var days = new Array((asyncData.relations.length + 1) * 60).fill('');
  days[0] = 'close';

  const data_list = [[{
    value: [0, 0, 30, authorId, authorName],
    label: {
      color: '#ff8d8d',
      position: 'inside'
    },
  }]];

  const relations = asyncData.relations;
  relations.map((relation, i) => {
    const authors = relation.authors;
    // 基准
    const base = (i * 60 + 40) * 360;
    // 偏移量
    // const offset = 0;
    const offset = 360 * 50 / (authors.length);
    // console.log('authors', authors);
    data_list.push([]);
    authors.map((element, j) => {
      const x = parseInt(((base + j * offset) / 360).toFixed());
      const y = parseInt(((base + j * offset) % 360).toFixed()) - Math.round(Math.random() * offset % 360 / 4);
      data_list[i+1].push({value: [x, y,
        9 + Math.round(Math.random() * 4),
        element.authorId, element.name], 
      })
    })
  });

  const name_list = ['Origin Author', 'Closed Authors', 'Not Too Close', 'Normal Close', 'Far Close'];
  const series = data_list.map((value, index) => {
    const seriesItem = Object.assign({}, seriesSet);
    seriesItem.name = name_list[index];
    seriesItem.data = value;
    return seriesItem;
  });



  const option = {
      title: {
        text: 'Relations',
      },
      legend: {
        left: 'right',
    },
      polar: {radius: '96%'},
      tooltip: {
        formatter: function (params) {
          return params.data.value[4];
        }
      },
      angleAxis: {
        type: 'category',
        data: degrees,
        boundaryGap: false,
        axisLine: {
          show: false
        },
        axisLabel: {
          show: false
        },
        axisTick:{       //y轴刻度线
          "show":false
        },
      },
      radiusAxis: {
        type: 'category',
        data: days,
        // boundaryGap: false,
        axisLine: {
          show: false
        },
        splitLine: {
          show: true
        },
        markLine: {
          show: false
        },
        axisLabel: {
          show: false,
        },
        axisTick:{       //y轴刻度线
          "show":false
        },
      },
      series
  };


  var myChart = echarts.init(document.getElementById('main'));

  myChart.setOption(option)

  myChart.on('click', function (params) {
    // 在用户点击后控制台打印数据的名称
    console.log(params);
  });
}

getData();